package com.pic.mgt.dao;

import java.util.Date;
import java.util.Map;
import java.util.Vector;

import org.springframework.dao.DataAccessException;

import com.pic.mgt.utils.IgxCollectionUtils;
import com.pic.mgt.utils.SqlScriptUtils;

public abstract class BusinessMgtDao extends BasicDao implements
		IBusinessMgtDao {
	
	private final String updateStatusByUnitNameSqlScript;
	private final String findStatusByUnitNameSqlScript;
	private final String updateCreatingTimeByUnitNameSqlScript;
	private final String updateStatusAndCreatingTimeByUnitNameSqlScript;
	
	public BusinessMgtDao() {
		super();
		
		// 建立 Update Status By Unit Name 的 Sql Script
		updateStatusByUnitNameSqlScript = createUpdateStatusByUnitNameSQLScript();
		
		// 建立 Query Status By Unit Name 的 Sql Script
		findStatusByUnitNameSqlScript = createFindStatusByUnitNameSqlScript();
		
		// 建立 update creating time by unit name 的 sql script
		this.updateCreatingTimeByUnitNameSqlScript = createUpdateCreatingTimeByUnitNameSqlScript();
		
		// 建立 update status and creating time by unit name 的 sql script
		this.updateStatusAndCreatingTimeByUnitNameSqlScript = createUpdateStatusAndCreatingTimeByUnitNameSqlScript();
	}
	
	private String createUpdateStatusAndCreatingTimeByUnitNameSqlScript() {
		Vector<String> updateFieldsVector = new Vector<String>();
		
		updateFieldsVector.add(_status);
		updateFieldsVector.add(_creatingTime);
		
		return SqlScriptUtils.createNamedParamUpdateScript(
										getTableName(),
										updateFieldsVector,
										_unitName);
	}
	
	private String createUpdateCreatingTimeByUnitNameSqlScript() {
		Vector<String> updateFieldsVector = new Vector<String>();
		
		updateFieldsVector.add(_creatingTime);
		
		return SqlScriptUtils.createNamedParamUpdateScript(
										getTableName(),
										updateFieldsVector,
										_unitName);
	}
	
	private String createFindStatusByUnitNameSqlScript() {
		Vector<String> queryFieldsVector = new Vector<String>();
		
		queryFieldsVector.add(BusinessMgtDao._status);
		
		return SqlScriptUtils.createNamedParamQueryScriptWithSingleCondition(
										getTableName(),
										queryFieldsVector,
										BusinessMgtDao._unitName);
	}
	
	private String createUpdateStatusByUnitNameSQLScript() {
		Vector<String> updateFieldsVector = new Vector<String>();
		
		updateFieldsVector.add(BusinessMgtDao._status);
		
		return
			SqlScriptUtils.createNamedParamUpdateScript(
										getTableName(),
										updateFieldsVector,
										BusinessMgtDao._unitName);
	}

	@Override
	public String findStatusByUnitName(String unitName)
			throws DataAccessException {
		return
		super.namedParameterJdbcTemplate.queryForObject(
				this.findStatusByUnitNameSqlScript,
				IgxCollectionUtils.convertToMap(new Object[] {BusinessMgtDao._unitName, unitName}),
				String.class);
	}

	@Override
	public int updateStatusByUnitName(String statusValue, String unitName)
			throws DataAccessException {
		if (null != statusValue && null != unitName) {
			Map<String, Object> updateValuesMap =
				IgxCollectionUtils.convertToMap(
						new Object[] {BusinessMgtDao._status, statusValue,
									  "__ConditionValue", unitName});
		
		return
			super.namedParameterJdbcTemplate.update(
							this.updateStatusByUnitNameSqlScript,
							updateValuesMap);
		}
		else {
			return 0;
		}
	}
	
	@Override
	public int updateStatusAndCreatingTimeByUnitName(String statusValue,
													 Date creatingTime, String unitName) throws DataAccessException {
		if (null != statusValue && null != creatingTime &&  null != unitName) {
			Map<String, Object> updateValuesMap =
				IgxCollectionUtils.convertToMap(
						new Object[] {_status, statusValue,
									  _creatingTime, creatingTime,
									  "__ConditionValue", unitName});
		
		return
			super.namedParameterJdbcTemplate.update(
							this.updateStatusAndCreatingTimeByUnitNameSqlScript,
							updateValuesMap);
		}
		else {
			return 0;
		}
	}
	
	@Override
	public int updateCreatingTimeByUnitName(Date creatingTime, String unitName)
			throws DataAccessException {
		if (null != creatingTime &&  null != unitName) {
			Map<String, Object> updateValuesMap =
				IgxCollectionUtils.convertToMap(
						new Object[] {_creatingTime, creatingTime,
									  "__ConditionValue", unitName});
		
		return
			super.namedParameterJdbcTemplate.update(
							this.updateCreatingTimeByUnitNameSqlScript,
							updateValuesMap);
		}
		else {
			return 0;
		}
	}
}
